package com.taiji.web.controller.workOrder;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.taiji.common.utils.uuid.IdUtils;
import com.taiji.workOrder.domain.Message;
import com.taiji.workOrder.domain.MessageBody;
import com.taiji.workOrder.domain.WorkOrder;
import com.taiji.workOrder.mapper.MessageMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.taiji.common.annotation.Log;
import com.taiji.common.core.controller.BaseController;
import com.taiji.common.core.domain.AjaxResult;
import com.taiji.common.enums.BusinessType;
import com.taiji.workOrder.service.IMyDealService;
import com.taiji.common.utils.poi.ExcelUtil;
import com.taiji.common.core.page.TableDataInfo;

/**
 * 我处理的Controller
 * 
 * @author taiji
 * @date 2023-03-19
 */
@RestController
@Api(tags="工单管理")
@RequestMapping("/workOrder/myDeal")
public class MyDealController extends BaseController
{
    @Autowired
    private IMyDealService myDealService;

    @Autowired
    private MessageMapper messageMapper;

    /**
     * 查询我处理的列表
     */
    @ApiOperation("查询我处理的列表")
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder myDeal)
    {
        startPage();
        List<WorkOrder> list = myDealService.selectMyDealList(myDeal);
        return getDataTable(list);
    }

    /**
     * 导出我处理的列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:export')")
    @Log(title = "我处理的", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder myDeal)
    {
        List<WorkOrder> list = myDealService.selectMyDealList(myDeal);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        util.exportExcel(response, list, "我处理的数据");
    }

    /**
     * 获取我处理的详细信息
     */
    @ApiOperation("获取我处理的详细信息")
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(myDealService.selectMyDealById(id));
    }

    /**
     * 新增我处理的
     */
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:add')")
    @Log(title = "我处理的", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder myDeal)
    {
        return toAjax(myDealService.insertMyDeal(myDeal));
    }

    /**
     * 修改我处理的
     */
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:edit')")
    @Log(title = "我处理的", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder myDeal)
    {
        return toAjax(myDealService.updateMyDeal(myDeal));
    }

    /**
     * 删除我处理的
     */
    @PreAuthorize("@ss.hasPermi('workOrder:myDeal:remove')")
    @Log(title = "我处理的", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(myDealService.deleteMyDealByIds(ids));
    }

    /**
     * 转单
     */


    @Log(title = "转单", businessType = BusinessType.INSERT)
    @PostMapping("/revert")
    public AjaxResult revert(@RequestBody WorkOrder workOrder) throws Exception {
        System.out.println("我处理的");
        return toAjax(myDealService.revert(workOrder));
    }

    /**
     * 完成工单
     */


    @Log(title = "完成工单", businessType = BusinessType.INSERT)
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody WorkOrder workOrder) throws Exception {

        int finish = myDealService.finish(workOrder.getWorkNumber());

        if (finish > 0) {
            //向工单创建人发送消息推送
            WorkOrder workOrder1 =  myDealService.selectWorkOrderByWorkNumber(workOrder.getWorkNumber());
            MessageBody messageBody = new MessageBody();
            messageBody.setId(IdUtils.randomUUID());
            messageBody.setSendTime(new Date());
            messageBody.setFromName("系统");
            messageBody.setToName(workOrder1.getCreateBy());
            messageBody.setContent(workOrder1.getName()+"工单完成");
            boolean flag =false;
            try {
                flag= WebSocketServer.sendMessageToUser(messageBody);

            } catch (IOException e) {
                e.printStackTrace();
            }
            if (!flag) {
                Message message = Message.getMessage(messageBody);
                message.setProjectCode(workOrder1.getProjectCode());
                message.setServerName("");
                message.setInfoType("完成");
                messageMapper.insertMessage(message);
            }
            return AjaxResult.success();
        }


        return AjaxResult.error();
    }
    /**
     * 关闭工单
     */


    @Log(title = "关闭工单", businessType = BusinessType.INSERT)
    @PostMapping("/close")
    public AjaxResult close(@RequestBody WorkOrder workOrder) throws Exception {
        return toAjax(myDealService.close(workOrder.getWorkNumber()));
    }
    /**
     * 受理工单
     */


    @Log(title = "受理工单", businessType = BusinessType.INSERT)
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody WorkOrder workOrder) throws Exception {
        return toAjax(myDealService.accept(workOrder.getWorkNumber()));
    }


}
